package pl.edu.agh.sr.lab3.zad2.impl.client;

import org.omg.CORBA.IntHolder;
import org.omg.CORBA.ORB;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.InvalidName;
import org.omg.CosNaming.NamingContextPackage.NotFound;

import pl.edu.agh.sr.lab3.zad2.generated.AFactory;
import pl.edu.agh.sr.lab3.zad2.generated.AFactoryHelper;
import pl.edu.agh.sr.lab3.zad2.generated.Item;
import pl.edu.agh.sr.lab3.zad2.generated.ItemA;
import pl.edu.agh.sr.lab3.zad2.generated.ItemAHelper;
import pl.edu.agh.sr.lab3.zad2.generated.ItemAlreadyExists;
import pl.edu.agh.sr.lab3.zad2.generated.ItemB;
import pl.edu.agh.sr.lab3.zad2.generated.ItemBHelper;
import pl.edu.agh.sr.lab3.zad2.generated.ItemBusy;
import pl.edu.agh.sr.lab3.zad2.generated.ItemNotExists;
import pl.edu.agh.sr.lab3.zad2.impl.common.Config;

public class Client {

	private ORB orb = null;
	private AFactory aFactory = null;

	private void init(String[] args) throws NotFound, CannotProceed,
			InvalidName {
		orb = ORB.init(args, null);

		// get NS
		org.omg.CORBA.Object nsRef = orb.string_to_object(Config.getIOR_Reference());
		NamingContextExt ncRef = NamingContextExtHelper.narrow(nsRef);

		// get aFactory
		org.omg.CORBA.Object factoryRef = ncRef
				.resolve_str(Config.getAFactoryName());
		System.out.println("Received aFactory ref: " + factoryRef);
		aFactory = AFactoryHelper.narrow(factoryRef);
	}

	private void sampleFactoryInvocationCreateA(String itemName)
			throws ItemAlreadyExists {
		System.out.println("------------- Start");
		Item item = aFactory.create_item(itemName, "itema");
		if (item == null)
			System.out.println("NULL SLHFKFKJDSHLKJDSHKJSDKJSDLKJSDBN");
		ItemA itemA = ItemAHelper.narrow(item);
		IntHolder val = new IntHolder();
		itemA.actionA((float) 2.4, val);
		System.out.println("itemA.name(): " + itemA.name());
		System.out.println("itemA.get_item_age(): " + itemA.get_item_age());
		System.out.println("res actionA(): " + val.value);
		System.out.println("============= End");
	}

	private void sampleFactoryTakeItem(String itemName) throws ItemNotExists,
			ItemBusy {
		System.out.println("------------- Start");
		Item item = aFactory.take_item(itemName);
		ItemA itemA = ItemAHelper.narrow(item);
		IntHolder val = new IntHolder();
		itemA.actionA((float) 2.4, val);
		System.out.println("itemA.name(): " + itemA.name());
		System.out.println("itemA.get_item_age(): " + itemA.get_item_age());
		System.out.println("res actionA(): " + val.value);
		System.out.println("============= End");
	}

	private void sampleFactoryReleaseItem(String itemName) throws ItemNotExists {
		aFactory.release_item(itemName);
	}

	public static void main(String[] args) {
		Client client = new Client();
		try {
			client.init(args);
			client.sampleFactoryInvocationCreateA("name1");
			client.sampleFactoryReleaseItem("name1");
			client.sampleFactoryTakeItem("name1");
			try {
				client.sampleFactoryInvocationCreateB("name1");
			} catch (ItemAlreadyExists e) {
				System.out.println("EXC: " + e.getMessage());
			}
			client.sampleFactoryInvocationCreateB("name2");
			client.sampleFactoryInvocationCreateB("name3");
			client.sampleFactoryReleaseItem("name3");
			

		} catch (Exception e) {
			System.err.println("Client exception: " + e.toString());
			e.printStackTrace();
		}
	}

	private void sampleFactoryInvocationCreateB(String itemName)
			throws ItemAlreadyExists {
		Item item = aFactory.create_item(itemName, "itemB");
		if (item == null)
			System.out.println("NULL SLHFKFKJDSHLKJDSHKJSDKJSDLKJSDBN");
		ItemB itemB = ItemBHelper.narrow(item);
		itemB.actionB("hello world");
		System.out.println("itemA.name(): " + itemB.name());
		System.out.println("itemA.get_item_age(): " + itemB.get_item_age());
		System.out.println("res actionB(): " + itemB.actionB("hello world"));
	}

}
